<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>信息修改申请管理</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.bundle.min.js"></script>
    <style>
        body { margin: 0; padding: 0; background: #f5f6fa; display: flex; min-height: 100vh; }
        .main-wrapper { display: flex; width: 100%; }
        .sidebar {
            width: 240px;
            background: linear-gradient(135deg, #4e73df 0%, #224abe 100%);
            padding: 0;
            box-shadow: 2px 0 8px rgba(0,0,0,0.07);
            min-height: 100vh;
            color: #fff;
            display: flex;
            flex-direction: column;
            align-items: stretch;
        }
        .sidebar h3 {
            color: #fff;
            font-weight: bold;
            text-align: center;
            padding: 32px 0 16px 0;
            letter-spacing: 2px;
            font-size: 1.3rem;
            margin-bottom: 0;
        }
        .sidebar ul {
            list-style: none;
            padding: 0 0 32px 0;
            margin: 0;
            flex: 1;
        }
        .sidebar ul li {
            margin: 0;
        }
        .sidebar ul li a {
            display: flex;
            align-items: center;
            padding: 14px 32px;
            color: #e3e6f0;
            text-decoration: none;
            font-size: 1.08rem;
            border-left: 4px solid transparent;
            transition: background 0.2s, color 0.2s, border-color 0.2s;
            border-radius: 0 24px 24px 0;
            margin-bottom: 2px;
        }
        .sidebar ul li a:hover,
        .sidebar ul li a.active {
            background: rgba(255,255,255,0.12);
            color: #fff;
            border-left: 4px solid #f6c23e;
            font-weight: bold;
        }
        .sidebar ul li a .icon {
            margin-right: 10px;
            font-size: 1.2em;
        }
        .main-content { flex-grow: 1; padding: 20px; background-color: #f5f6fa; }
        .info-card { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
    </style>
</head>
<body>
<div class="main-wrapper">
    <div class="sidebar">
        <h3>医院病例管理系统</h3>
        <ul>
<%--            <li><a href="/adminStatistics">统计分析</a></li>--%>
            <li><a href="/adminUserList">用户管理</a></li>
            <li><a href="/adminMedicalRecordList">病历管理</a></li>
            <li><a href="/adminPersonalCenter">个人信息管理</a></li>
            <li><a href="/adminMedicalTemplate">病历模板管理</a></li>
            <li><a href="/adminUpdateRequest" class="active">信息修改申请</a></li>
        </ul>
    </div>
    <div class="main-content">
        <div class="info-card">
            <h2>信息修改申请管理</h2>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>申请ID</th>
                    <th>用户ID</th>
                    <th>字段</th>
                    <th>原值</th>
                    <th>新值</th>
                    <th>申请时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="requestTableBody">
                </tbody>
            </table>
            <div id="loading" class="text-center">加载中...</div>
            <div id="error" class="alert alert-danger" style="display: none;">加载失败</div>
        </div>
    </div>
</div>
<script>
$(function(){
    function loadRequests() {
        $('#loading').show();
        $('#error').hide();
        $.ajax({
            url: '/admin/updateRequest/list',
            type: 'GET',
            success: function(res) {
                $('#loading').hide();
                if(res.code === 0 && res.data) {
                    const $tbody = $('#requestTableBody');
                    $tbody.empty();
                    if (res.data.length === 0) {
                        $tbody.append('<tr><td colspan="7" class="text-center">暂无待审核申请</td></tr>');
                        return;
                    }
                    res.data.forEach(req => {
                        const fieldLabel = req.fieldName === 'history' ? '既往史' : (req.fieldName === 'allergy' ? '过敏史' : req.fieldName);
                        const $row = $('<tr>');
                        $row.append($('<td>').text(req.id));
                        $row.append($('<td>').text(req.userId));
                        $row.append($('<td>').text(fieldLabel));
                        $row.append($('<td>').text(req.oldValue || '-'));
                        $row.append($('<td>').text(req.newValue || '-'));
                        $row.append($('<td>').text(req.createTime ? new Date(req.createTime).toLocaleString() : '-'));
                        const $approveBtn = $('<button>').addClass('btn btn-sm btn-success me-2 approve-btn').text('通过').attr('data-id', req.id);
                        const $rejectBtn = $('<button>').addClass('btn btn-sm btn-danger reject-btn').text('拒绝').attr('data-id', req.id);
                        $row.append($('<td>').append($approveBtn).append($rejectBtn));
                        $tbody.append($row);
                    });
                } else {
                    $('#error').text(res.message || '获取申请列表失败').show();
                }
            },
            error: function(){
                $('#loading').hide();
                $('#error').text('请求失败，请稍后重试').show();
            }
        });
    }
    // 审核通过
    $('#requestTableBody').on('click', '.approve-btn', function(){
        const id = $(this).data('id');
        if (confirm('确定通过该申请吗？')) {
            $.ajax({
                url: '/admin/updateRequest/review',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({id: id, status: 1}),
                success: function(res) {
                    if (res.code === 0) {
                        alert('操作成功');
                        loadRequests();
                    } else {
                        alert(res.message || '操作失败');
                    }
                }
            });
        }
    });
    // 审核拒绝
    $('#requestTableBody').on('click', '.reject-btn', function(){
        const id = $(this).data('id');
        if (confirm('确定拒绝该申请吗？')) {
            $.ajax({
                url: '/admin/updateRequest/review',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({id: id, status: 2}),
                success: function(res) {
                    if (res.code === 0) {
                        alert('操作成功');
                        loadRequests();
                    } else {
                        alert(res.message || '操作失败');
                    }
                }
            });
        }
    });
    loadRequests();
});
</script>
</body>
</html> 